//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a sample for BCGControlBar Library Professional Edition
// Copyright (C) 1998-2016 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions 
// of the accompanying license agreement.
//*******************************************************************************
//
// ChartSmartLabelsView.cpp : implementation file
//

#include "stdafx.h"
#include "bcgpchartexample.h"
#include "ChartSmartLabelsView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define MAX_POINT_SERIES		4
#define MAX_POINTS_IN_SERIES	20
#define MIN_Y_VAL				0.
#define MAX_Y_VAL				20.

#define X_RANGE_PER_SERIES		3
#define X_VALUE_GRADE			100.

/////////////////////////////////////////////////////////////////////////////
// CChartSmartLabelsView

IMPLEMENT_DYNCREATE(CChartSmartLabelsView, CBCGPChartExampleView)

CChartSmartLabelsView::CChartSmartLabelsView()
	: CBCGPChartExampleView(CChartSmartLabelsView::IDD)
{
	//{{AFX_DATA_INIT(CChartSmartLabelsView)
	m_bArrangeByAngle = TRUE;
	m_bArrangeByDistance = TRUE;
	m_nAngleOffset = 15;
	m_nDistancePercent = 100;
	m_strAngleOffsetLabel = _T("");
	m_strDistancePercentLabel = _T("");
	//}}AFX_DATA_INIT
}

CChartSmartLabelsView::~CChartSmartLabelsView()
{
}

void CChartSmartLabelsView::DoDataExchange(CDataExchange* pDX)
{
	CBCGPChartExampleView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChartSmartLabelsView)
	DDX_Control(pDX, IDC_DISTANCE_PERCENT, m_wndDistancePercent);
	DDX_Control(pDX, IDC_ANGLE_OFFSET, m_wndAngleOffset);
	DDX_Control(pDX, IDC_CHART, m_wndChart);
	DDX_Check(pDX, IDC_ARRANGE_BY_ANGLE, m_bArrangeByAngle);
	DDX_Check(pDX, IDC_ARRANGE_BY_DISTANCE, m_bArrangeByDistance);
	DDX_Slider(pDX, IDC_ANGLE_OFFSET, m_nAngleOffset);
	DDX_Slider(pDX, IDC_DISTANCE_PERCENT, m_nDistancePercent);
	DDX_Text(pDX, IDC_ANGLE_OFFSET_LABEL, m_strAngleOffsetLabel);
	DDX_Text(pDX, IDC_DISTANCE_PERCENT_LABEL, m_strDistancePercentLabel);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChartSmartLabelsView, CBCGPChartExampleView)
	//{{AFX_MSG_MAP(CChartSmartLabelsView)
	ON_BN_CLICKED(IDC_ARRANGE_BY_ANGLE, OnUpdateChart)
	ON_BN_CLICKED(IDC_ARRANGE_BY_DISTANCE, OnUpdateChart)
	ON_WM_HSCROLL()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChartSmartLabelsView diagnostics

#ifdef _DEBUG
void CChartSmartLabelsView::AssertValid() const
{
	CBCGPChartExampleView::AssertValid();
}

void CChartSmartLabelsView::Dump(CDumpContext& dc) const
{
	CBCGPChartExampleView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChartSmartLabelsView message handlers

void CChartSmartLabelsView::OnInitialUpdate() 
{
	CBCGPChartExampleView::OnInitialUpdate();
	
	if (m_bIsReady)
	{
		return;
	}

	m_bIsReady = TRUE;

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	m_wndDistancePercent.SetRange(100, 1000, TRUE);
	m_wndAngleOffset.SetRange(0, 360, TRUE);

	pChart->SetChartType(BCGPChartLine, BCGP_CT_SIMPLE);
	pChart->SetLegendPosition(BCGPChartLayout::LP_NONE);

	for (int i = 0; i < MAX_POINT_SERIES; i++)
	{
		int nXRangeLow = (int)(i - MAX_POINT_SERIES / 2.) * X_RANGE_PER_SERIES;
		int nXRangeHigh = nXRangeLow + X_RANGE_PER_SERIES;

		for (int j = 0; j < MAX_POINTS_IN_SERIES; j++)
		{
			double dblX = Rand(nXRangeLow / X_VALUE_GRADE, nXRangeHigh / X_VALUE_GRADE);
			double dblY = Rand(MIN_Y_VAL, MAX_Y_VAL);
			pChart->AddChartDataYX(dblY, dblX, i);
		}

		CBCGPChartSeries* pSeries = pChart->GetSeries(i);
		ASSERT_VALID(pSeries);

		BCGPChartFormatSeries style = pSeries->GetSeriesFormat();
		style.m_curveType = BCGPChartFormatSeries::CCT_NO_LINE;

		pSeries->SetSeriesFormat(style);

		pSeries->SetDataLabelDataFormat(_T("%.2f"));
	}

	pChart->ShowDataMarkers();
	pChart->ShowDataLabels();

	OnUpdateChart();
}

void CChartSmartLabelsView::OnUpdateChart() 
{
	UpdateData();

	m_strAngleOffsetLabel.Format(_T("%d"), m_nAngleOffset);
	m_strDistancePercentLabel.Format(_T("%d %%"), m_nDistancePercent);

	m_wndDistancePercent.EnableWindow(m_bArrangeByDistance);
	m_wndAngleOffset.EnableWindow(m_bArrangeByAngle);

	UpdateData(FALSE);

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	BCGPChartSmartLabelParams params;

	params.m_nAngleOffset = m_nAngleOffset;
	params.m_nMaxAllowedDistancePercent = m_nDistancePercent;
	params.m_bArrangeByAngle = m_bArrangeByAngle;
	params.m_bArrangeByDistance = m_bArrangeByDistance;

	pChart->SetSmartLabelsParams(params);
}

void CChartSmartLabelsView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	if (pScrollBar != NULL)
	{
		OnUpdateChart();
		return;
	}
	
	CBCGPChartExampleView::OnHScroll(nSBCode, nPos, pScrollBar);
}
